/*
  AHT10 - A Humidity Library for Arduino.

  Supported Sensor modules:
    AHT10-Breakout Module - https://www.aliexpress.com/item/33002710848.html

  Created by Thinary Eletronic at Modern Device on April 2019.

 * This file is part of Thinary_AHT10.
 *
 * Thinary_AHT10 is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or(at your option) any later version.
 *
 * Sodaq_SHT2x is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Thinary_AHT10.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#ifndef AHT10_H
#define AHT10_H

#include <Arduino.h>
#include <stdint.h>

typedef enum
{
  eAHT10Address_default = 0x38,
  eAHT10Address_Low = 0x38,
  eAHT10Address_High = 0x39,
} HUM_SENSOR_T;

typedef unsigned char Sensor_CMD;

#define BUFFER_LENGTH 32

class AHT10Class
{
private:
  unsigned long readSensor(boolean GetDataCmd);
  uint8_t AHT10_address;
  // SDA和SCL引脚
  uint8_t SCL_pin, SDA_pin;
  // IIC缓冲区
  // rx是接收(receive)
  uint8_t rxBuffer[BUFFER_LENGTH];
  uint8_t rxBufferIndex;
  uint8_t rxBufferLength;
  // 指明是否在传输
  uint8_t transmitting;
  // 错误类型
  uint8_t error;
  // IIC函数
  uint8_t i2c_addr;
  void IICbegin(uint8_t sdapin, uint8_t sclpin);
  void IICbeginTransmission(uint8_t address);
  void IICbeginTransmission(int address);
  uint8_t IICendTransmission(uint8_t sendStop);
  uint8_t IICendTransmission(void);
  void IICstart(void);
  void IICrestart(void);
  void IICstop(void);
  uint8_t IICread(uint8_t last);
  bool IICwrite(uint8_t data);
  void IICwrite(uint8_t *data, uint8_t quantity);
  uint8_t IICrequestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop);
  uint8_t IICrequestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop);
  uint8_t IICrequestFrom(int address, int quantity, int sendStop);
  uint8_t IICrequestFrom(uint8_t address, uint8_t quantity);
  uint8_t IICrequestFrom(int address, int quantity);
  int IICavailable(void);
  int IICread2(void);
  int IICpeek(void);

public:
  AHT10Class(uint8_t sdaPin, uint8_t sclPin);
  boolean begin(unsigned char _AHT10_address = eAHT10Address_default);
  float GetHumidity(void);
  float GetTemperature(void);
  float GetDewPoint(void);
  unsigned char readStatus(void);
  void Reset(void);
};

#endif
